<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Suport_model extends Model 
{
	/**
	 * @var unknown_type
	 */
	var $id_suport  = 0;  
	/**
	 * @var unknown_type
	 */
	
	var $name = null ; 
	/**
	 * @var unknown_type
	 */
	var $nick_name = null ; 
	/**
	 * @var unknown_type
	 */
	 var $provider = null ; 
	/**
	 * @var unknown_type
	 */
	 var $department = null ; 
	/**
	 * @var unknown_type
	 */
	var $order_view = null ; 
	/**
	 * @var unknown_type
	 */
	var $active = 0;
	/*
	 * @access	public
	 */
	
	function Suport_model()
	{
		parent::Model();
		
	}
		
	function insert(){
		$sql = "INSERT INTO gtb_suport_online (nick_name, name, provider, department, order_view, active)
				VALUES(".
				$this->db->escape($this->nick_name).", " .
				$this->db->escape($this->name).", " .
				$this->db->escape($this->provider).", " .
				$this->db->escape($this->department).", " .
				intval($this->order_view).", " .
				intval($this->active).
				")";
				
		$this->db->query($sql);
		if($this->db->affected_rows())
			return $this->db->insert_id(); 
		return 0;
	}
	
	function update($adv){
		$sql = "UPDATE gtb_suport_online
				SET ".
				" nick_name = " .  $this->db->escape($adv->nick_name) . "," .
				" name = " .  $this->db->escape($adv->name) . "," .
				" provider = " .  $this->db->escape($adv->provider) . ", " .
				" department = " .  $this->db->escape($adv->department) . ", " .
				" order_view = " .  intval($adv->order_view) . ", " .
				" active = " .  intval($adv->active)  .
				" WHERE id_suport = " .intval($adv->id_suport);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = trim(implode(',', $id));
		if($ids=='') return;
						
		$sql = "DELETE FROM gtb_suport_online WHERE id_suport IN ($ids)";
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAll($order_view = '', $offset = 0, $limit=0){
		$result = null;
		if($limit)
			$sql = "SELECT a.*,".
			 		" (SELECT MAX(t3.order_view) FROM gtb_suport_online t3) as max_order, 
					(SELECT MIN(t4.order_view) FROM gtb_suport_online t4) as min_order, 
					(SELECT (t5.id_suport) FROM gtb_suport_online t5 WHERE t5.order_view > a.order_view LIMIT 0,1) as id_next,
					(SELECT (t6.id_suport) FROM gtb_suport_online t6 WHERE t6.order_view < a.order_view ORDER BY t6.order_view DESC LIMIT 0,1) as id_preview " .
					" FROM gtb_suport_online a".
					" ORDER BY a.order_view " .  $order_view . 
				 	" LIMIT " . intval($offset) . ", " . intval($limit);
		else
			$sql = "SELECT a.*".
					" FROM gtb_suport_online a".
					" WHERE a.active = 1".
					" ORDER BY a.order_view " .  $order_view ;
		//echo $sql;	
		$query = $this->db->query($sql);
		$result = $query->result();
      
		return $result;
	}
	
	function getAllCount(){
		$sql = 'SELECT COUNT(*) as num_record
				FROM gtb_suport_online a';
		
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->num_record;
		}
       
		return 0;
		
	}
	
	function getMaxOrder(){
		
		$sql = "SELECT MAX(order_view) as order_max
				FROM gtb_suport_online ";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->order_max ;
		}
       
		return 0;
	}
	
	function getById($id_adv){
		$sql = 'SELECT *
				FROM gtb_suport_online a
				WHERE a.id_suport = ' . intval($id_adv);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	
		
//	
}

// EOF
